# DjangoDE, an integrated development environment for Django
# Copyright (C) 2010-2011 Andrew Wilkinson
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import unittest

from djangode.data.codemodel.builtins import int_class, string_class
from djangode.data.codemodel.namespace import Namespace

class TestNamespace(unittest.TestCase):
    def setUp(self):
        self.namespace = Namespace(None)

    def test_set_new_variable(self):
        self.namespace.set_variable("a", int_class.call())

        self.assert_(len(self.namespace.variables) == 1)

    def test_set_variable_new_type(self):
        self.namespace.set_variable("a", int_class.call())
        self.namespace.set_variable("a", string_class.call())

        self.assert_(len(self.namespace.variables) == 1)
        self.assert_(len(self.namespace.variables["a"].value.type) == 2, self.namespace.variables["a"].value.type)

    def test_set_variable_same_type(self):
        self.namespace.set_variable("a", int_class.call())
        self.namespace.set_variable("a", int_class.call())

        self.assert_(len(self.namespace.variables) == 1)
        self.assert_(len(self.namespace.variables["a"].value.type) == 1, self.namespace.variables["a"].value)

    def test_set_second_variable(self):
        self.namespace.set_variable("a", int_class.call())
        self.namespace.set_variable("b", int_class.call())

        self.assert_(len(self.namespace.variables) == 2)

    def test_get_variable(self):
        self.namespace.set_variable("a", int_class.call())

        self.assert_(self.namespace.get_variable("a").value.is_instance_of(int_class), (self.namespace.get_variable("a").value, int_class))

    def test_get_attributes(self):
        self.namespace.set_variable("a", int_class.call())
        self.namespace.set_variable("b", int_class.call())

        self.assert_(len(self.namespace.get_attributes()) == 2)

    def test_get_attributes_with_prefix(self):
        self.namespace.set_variable("abc", int_class.call())
        self.namespace.set_variable("abd", int_class.call())
        self.namespace.set_variable("acd", int_class.call())

        self.assert_(len(self.namespace.get_attributes("ab")) == 2)
